package com.mapproject.industrymap.service.impl;

import com.alibaba.fastjson.JSON;
import com.mapproject.industrymap.entity.SysUploadFileEntity;
import com.mapproject.industrymap.mapper.BaseInfoMapper;
import com.mapproject.industrymap.service.ShpTransforPgService;
import com.mapproject.industrymap.service.SysUploadFileService;
import com.mapproject.industrymap.utils.DataItemShapefile;
import com.mapproject.industrymap.utils.DataPgdump;
import com.mapproject.industrymap.utils.ReturnValue;
import com.mapproject.industrymap.utils.UnZipFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class ShpTransforPgServiceImpl implements ShpTransforPgService {
    @Autowired
    SysUploadFileService sysUploadFileService;
    @Autowired
    UnZipFile unZipFile;
    @Autowired
    BaseInfoMapper baseInfoMapper;
    @Value("${file.uploadFolder}")
    private String uploadFolder;
    @Value("${file.projectFileDir}")
    private String projectFileDir;

    @Override
    public  String insertShpData(String path){
        DataItemShapefile dataItemShapefile=new DataItemShapefile();
        return dataItemShapefile.readSHP("","",path);
    }

    public  String uploadInsertShpData(String tableName,String fileName){

        String uploadPath=projectFileDir;
        String filePath = uploadFolder+uploadPath+fileName+"/"+fileName+".shp";//shp文件地址
        DataItemShapefile dataItemShapefile=new DataItemShapefile();
        return dataItemShapefile.readSHP(tableName,fileName,filePath);
    }

    public ReturnValue insertBigShpData(MultipartFile file,String tablename, String rowguid){
      int sum=baseInfoMapper.querytablename(tablename);
      if(sum>0){
          return new ReturnValue(500,"表已存在");
      }
        ReturnValue ret=  sysUploadFileService.uploadfile(file,rowguid);
        if(ret.getCode()==200){
            SysUploadFileEntity sysUploadFileEntity= JSON.parseObject(JSON.toJSONString(ret.getData()), SysUploadFileEntity.class);
            String path=sysUploadFileEntity.getServerfileurl();
            List<String> dataret= unZipFile.batchadd(path);
            ReturnValue returnValue=new ReturnValue(500,"未执行");
            for (String eve:dataret){
                String suffixName = eve.substring(eve.lastIndexOf(".")+1);
                if(suffixName.equals("shp")){
                    DataPgdump dataPgdump=new DataPgdump();
                    ReturnValue result= dataPgdump.impshpdata(eve,tablename);
                    returnValue=result;
                    break;
                }
            }
            return returnValue;
        }else{
            return new ReturnValue(500,"上传文件失败");
        }

    }
}
